#!/bin/bash
 #克隆 uir 仓库
# 尝试克隆仓库，最多重试5次
MAX_ATTEMPTS=20
# 初始尝试次数为1
ATTEMPT_NUMBER=1
# 仓库源码路径
# 克隆源码
function clone_repo(){
until git $1; do
    if [ $ATTEMPT_NUMBER -lt $MAX_ATTEMPTS ]; then
        echo $2
        echo "克隆失败，第 $ATTEMPT_NUMBER 次重试..."
        let ATTEMPT_NUMBER++
        sleep 10 # 等待5秒后再重试
    else
        echo $2
        echo "重试次数已到，停止尝试。"
        exit 1
    fi
done

}

#  编译 openssl
function build_openssl() {
  # 设置 openssl 源码路径
  OPENSSL_DIR=${BUILD_DIR}/openssl
  # 判断 openssl 源码路径 文件夹是否存在
  if [ ! -d ${OPENSSL_DIR} ]; then
    # 切换到 build 目录
    cd ${BUILD_DIR}
    #克隆 openssl
    clone_repo "clone -b OpenSSL_1_1_1-stable https://gitcode.com/sound/openssl.git ${OPENSSL_DIR}" "克隆 openssl"
  fi
  #进入 openssl 源码路径
  cd ${OPENSSL_DIR}
    echo "编译 openssl"
    ./config --prefix=${LIB_DIR} no-asm shared zlib
<<<<<<< HEAD
    if [ $? -eq 0 ]; then
          make clean
          make -j$(nproc)
          make install
    fi
=======
    make clean
    make -j$(nproc)
    make install
>>>>>>> origin/main
}
#  编译 opencv
function build_opencv() {
  # 设置 opencv 源码路径
  OPENCV_DIR=${BUILD_DIR}/opencv
  OPENCV_CONTRID_DIR=${BUILD_DIR}/opencv_contrib
  # 判断 opencv 源码路径 文件夹是否存在
  if [ ! -d ${OPENCV_DIR} ]; then
    # 切换到 build 目录
    cd ${BUILD_DIR}
    #克隆 opencv
    clone_repo "clone -b 4.5.5 https://gitcode.com/sound/opencv.git ${OPENCV_DIR}" "克隆 opencv"
  fi
   # 判断 opencv_contrib 源码路径 文件夹是否存在
    if [ ! -d ${OPENCV_CONTRID_DIR} ]; then
      # 切换到 build 目录
      cd ${BUILD_DIR}
      #克隆 openssl
      clone_repo "clone -b 4.5.5 https://gitcode.com/sound/opencv_contrib.git ${OPENCV_CONTRID_DIR}" "克隆 opencv_contrib"
    fi
  # 判断 opencv依赖是否存在 源码路径 文件夹是否存在
  OPENCV_LIB_DOWNLOAD=${BUILD_DIR}/downloads
    if [ ! -d ${OPENCV_LIB_DOWNLOAD} ]; then
      # 切换到 build 目录
      cd ${BUILD_DIR}
      #克隆 opencv 依赖库
      clone_repo "clone https://gitcode.com/sound/opencv_make_downloads.git ${OPENCV_LIB_DOWNLOAD}" "克隆 opencv 依赖库"
    fi
  #进入 opencv 源码路径
  cd ${OPENCV_DIR}
  rm -rf build
  mkdir build && cd build
  cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=${LIB_DIR} \
        -D INSTALL_C_EXAMPLES=ON \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D OPENCV_GENERATE_PKGCONFIG=ON \
        -D WITH_IPP=OFF \
        -D WITH_FFMPEG=ON \
        -D FFMPEG_INCLUDE_DIRS="${LIB_DIR}/include/" \
        -D FFMPEG_LIBRARY_DIRS="${LIB_DIR}/lib/" \
        -D OPENCV_ENABLE_NONFREE=ON \
        -D BUILD_opencv_videoio=ON \
        -D BUILD_opencv_highgui=ON \
        -D WITH_TBB=ON \
        -D WITH_GTK=ON \
        -D WITH_V4L=ON \
        -D OPENCV_DOWNLOAD_PATH=${OPENCV_LIB_DOWNLOAD} \
        -D OPENCV_EXTRA_MODULES_PATH=${OPENCV_CONTRID_DIR}/modules \
        -D BUILD_EXAMPLES=ON ..
    echo "编译 opencv"
    make -j$(nproc)
    if [ $? -eq 0 ]; then
        echo " opencv 编译成功"
        make install
    else
        echo "opencv 编译失败"

    fi

    echo "编译 opencv 完成"
}
#  编译 x264
function build_x264() {
  # 设置 x264 源码路径
  X264_DIR=${BUILD_DIR}/x264
  # 判断 x264 源码路径 文件夹是否存在
  if [ ! -d ${X264_DIR} ]; then
    # 切换到 build 目录
    cd ${BUILD_DIR}
    #克隆 x264
    clone_repo "clone https://gitcode.com/sound/x264.git ${X264_DIR}" "克隆 x264"
  fi
  #进入 x264 源码路径/mnt/nas/work/make/git-work/build/x264/configure
  cd ${X264_DIR}
    echo "编译 x264"
    ./configure --prefix=${LIB_DIR} --disable-asm --enable-shared --enable-static
<<<<<<< HEAD
    if [ $? -eq 0 ]; then
          make clean
          make -j$(nproc)
          make install
    fi
=======
    make clean
    make -j$(nproc)
    make install
>>>>>>> origin/main
}

#  编译 x265
function build_x265() {
  # 设置 x265 源码路径
  X265_DIR=${BUILD_DIR}/x265
  # 判断 x264 源码路径 文件夹是否存在
  if [ ! -d ${X265_DIR} ]; then
    # 切换到 build 目录
    cd ${BUILD_DIR}
    #克隆 x265
    clone_repo "clone https://gitcode.com/sound/x265.git ${X265_DIR}" "克隆 x265"
  fi
  #进入 x265 源码路径
  cd ${X265_DIR}/build/linux
    echo "编译 x265"
    cmake -G "Unix Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${LIB_DIR} \
    -DCMAKE_ANDROID_STL_TYPE=c++_shared \
    -DENABLE_SHARED=on \
    -DCMAKE_BUILD_TYPE=Release \
    -DENABLE_CLI=off ../../source
<<<<<<< HEAD
    if [ $? -eq 0 ]; then
          make clean
          make -j$(nproc)
          make install
    fi
=======
    make clean
    make -j$(nproc)
    make install
>>>>>>> origin/main
}

#  编译 speex
function build_speex() {
  # 设置 speex 源码路径
  SPEEX_DIR=${BUILD_DIR}/speex
  # 判断 speex 源码路径 文件夹是否存在
  if [ ! -d ${SPEEX_DIR} ]; then
    # 切换到 build 目录
    cd ${BUILD_DIR}
    #克隆 speex
    clone_repo "clone https://gitcode.com/sound/speex.git ${SPEEX_DIR}" "克隆 speex"
  fi
  #进入 speex 源码路径
  cd ${SPEEX_DIR}
    echo "编译 speex"
    ./configure --prefix=${LIB_DIR} --enable-shared --enable-static
<<<<<<< HEAD
    if [ $? -eq 0 ]; then
          make clean
          make -j$(nproc)
          make install
    fi
=======
    make clean
    make -j$(nproc)
    make install
>>>>>>> origin/main
}

#  编译 mp3lame
function build_mp3lame() {
  # 设置 mp3lame 源码路径
  MP3LAME_DIR=${BUILD_DIR}/mp3lame
  # 判断 mp3lame 源码路径 文件夹是否存在
  if [ ! -d ${MP3LAME_DIR} ]; then
    # 切换到 build 目录
    cd ${BUILD_DIR}
    #克隆 mp3lame
    clone_repo "clone https://gitcode.com/sound/mp3lame.git ${MP3LAME_DIR}" "克隆 mp3lame"
  fi
  #进入 mp3lame 源码路径
  cd ${MP3LAME_DIR}
    echo "编译 mp3lame"
    ./configure --prefix=${LIB_DIR} --enable-shared --enable-static
<<<<<<< HEAD
    if [ $? -eq 0 ]; then
          make clean
          make -j$(nproc)
          make install
    fi
=======
    make clean
    make -j$(nproc)
    make install
>>>>>>> origin/main
}

#  编译 fdk-aac
function build_fdk-aac() {
  # 设置 fdk-aac 源码路径
  FDKAAC_DIR=${BUILD_DIR}/fdkaac
  # 判断 fdk-aac 源码路径 文件夹是否存在
  if [ ! -d ${FDKAAC_DIR} ]; then
    # 切换到 build 目录
    cd ${BUILD_DIR}
    #克隆 mp3lame
    clone_repo "clone https://gitcode.com/sound/fdk-aac.git ${FDKAAC_DIR}" "克隆 fdk-aac"
  fi
  #进入 fdk-aac 源码路径
  cd ${FDKAAC_DIR}
    echo "编译 fdk-aac"
    ./autogen.sh
    ./configure --prefix=${LIB_DIR} --enable-shared
<<<<<<< HEAD
    if [ $? -eq 0 ]; then
          make clean
          make -j$(nproc)
          make install
    fi

=======
    make clean
    make -j$(nproc)
    make install
>>>>>>> origin/main
}

#  编译 ffmpeg
function build_ffmpeg() {
  # 设置 fdk-aac 源码路径
  FFMPEG_DIR=${BUILD_DIR}/ffmpeg
  # 判断 ffmpeg 源码路径 文件夹是否存在
  if [ ! -d ${FFMPEG_DIR} ]; then
    # 切换到 build 目录
    cd ${BUILD_DIR}
    #克隆 mp3lame
    clone_repo "clone -b ${1} https://gitcode.com/sound/ffmpeg.git ${FFMPEG_DIR}" "克隆 ffmpeg"
  fi
  #进入 ffmpeg 源码路径
  cd ${FFMPEG_DIR}
 # 设置动态库路径
 export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${LIB_DIR}/lib/pkgconfig
# 配置 编译选项 ffmpeg
./configure \
--prefix=${LIB_DIR} \
--extra-cflags="-I${LIB_DIR}/include"  \
--extra-ldflags="-L${LIB_DIR}/lib" \
--pkg-config-flags="--define-variable=prefix=${LIB_DIR}" \
--pkg-config="pkg-config --shared" \
--arch=x86_64 \
--disable-static \
--disable-doc \
--enable-gpl \
--enable-debug \
--enable-filter=delogo \
--enable-shared \
--enable-hardcoded-tables \
--enable-optimizations \
--enable-version3 \
--enable-pthreads \
--enable-nonfree \
--enable-libx264 \
--enable-libx265 \
--enable-libmp3lame \
--enable-libfdk-aac \
--enable-libspeex \
--enable-openssl

if [ $? -eq 0 ]; then
<<<<<<< HEAD
    make clean
    make -j$(nproc)
    make install
fi
=======
    echo "配置 ffmpeg 成功"
else
    echo "配置 ffmpeg 失败"

fi
    # 编译
 #   make clean
  #  make -j$(nproc)
   # make install
>>>>>>> origin/main
}
















